package com.tywj.buscustomerapp.view.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.DaysBean;
import com.tywj.buscustomerapp.model.bean.MonthTicketBean;
import com.tywj.buscustomerapp.presenter.BasePresenter;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.ToastUtils;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.item.MyTravelMonthItem;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyTravelMonthTicketFragment extends BaseMvpFragment {
    RecyclerView recyclerView;
    SmartRefreshLayout srl;
    RelativeLayout errorLayout;
    private CommonRcvAdapter<DaysBean> adapter;
    private List<DaysBean> beans;
    ImageView right_img;
    private Animation translateAnimation;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_my_ticket;
    }

    @Override
    public void initView(View view) {
//        right_img = view.findViewById(R.id.right_img);
        recyclerView = view.findViewById(R.id.recycler);
        srl = view.findViewById(R.id.refresh);
        errorLayout = view.findViewById(R.id.error);
        adapter  = new CommonRcvAdapter<DaysBean>(beans) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                return new MyTravelMonthItem();
            }
        };
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Observable.create(new ObservableOnSubscribe<List<DaysBean>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<DaysBean>> e){
                        try {
                            String msg = WebServicrUtils.getInstance().getMyDays(SPUtils.getUserPhone(getContext()));
                            Log.i("taylor============>",msg);
                            InputStream in_nocode = new ByteArrayInputStream(msg.getBytes("UTF-8"));
                            List<DaysBean> list = WebServicrUtils.getInstance().getMyDaysTicket(in_nocode);
                            List<DaysBean> list1 = new ArrayList<>();
                            if (list!=null && list.size()>0){
                                for (int i = 0; i < list.size(); i++) {
                                    if (!list.get(i).getState().equals("待支付")){
                                        list1.add(list.get(i));
                                    }
                                }
                                if (list1.size()<1){
                                    Log.i("taylor","周期票为null1");
                                }else {
                                    e.onNext(list1);
                                }
                            }else{
                                Log.i("taylor","周期票为null2");
                                e.onError(null);
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                            e.onError(null);
                        } catch (XmlPullParserException e1) {
                            e1.printStackTrace();
                            e.onError(null);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                            e.onError(null);
                        }
                    }
                }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<DaysBean>>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(List<DaysBean> s) {
                                if (srl.isRefreshing())
                                    srl.finishRefresh();
                                adapter.setData(s);
                                adapter.notifyDataSetChanged();
                            }

                            @Override
                            public void onError(Throwable e) {
                                if (srl.isRefreshing())
                                    srl.finishRefresh();
                                errorLayout.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });

    }

    @Override
    public void initData() {
        srl.autoRefresh();
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                switch (newState){
//                    case 0:
//                        doAnimaiton(true);
//                        break;
//                    case 1:
//                        doAnimaiton(false);
//                        break;
//                }
//
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//            }
//        });
    }

    @Override
    protected BasePresenter bindPresenter() {
        return null;
    }

    private void doAnimaiton(boolean isShow){
        if (isShow){
            int[] position = new int[2];
            right_img.getLocationOnScreen(position);
            translateAnimation = new TranslateAnimation(position[0], 0, 0, 0);
            translateAnimation.setDuration(500);
            translateAnimation.setFillAfter(true);
            right_img.startAnimation(translateAnimation);
            isShow= false;
        }else {
            int[] position = new int[2];
            right_img.getLocationOnScreen(position);
            translateAnimation = new TranslateAnimation(0, position[0], 0, 0);
            translateAnimation.setDuration(500);
            translateAnimation.setFillAfter(true);
            right_img.startAnimation(translateAnimation);
            isShow= true;
        }
    }

}
